package cn.zb.zbrhback.basic.service.impl;

import cn.zb.zbrhback.basic.mapper.BaseMapper;
import cn.zb.zbrhback.basic.service.IBaseService;
import cn.zb.zbrhback.basic.util.BaseQuery;
import cn.zb.zbrhback.basic.util.PageResult;
import cn.zb.zbrhback.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T o) {
        baseMapper.insert(o);
    }

    @Override
    @Transactional
    public void deleteById(Serializable id) {
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void update(T o) {
        baseMapper.update(o);
    }

    @Override
    public T getById(Serializable id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List getAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageResult<T> findPage(BaseQuery baseQuery) {
        Long count = baseMapper.loadCount(baseQuery);
        if(count > 0){
            List<T> employees = baseMapper.loadPage(baseQuery);
            return new PageResult<>(count,employees,"查询成功");
        }
        return new PageResult<>();
    }

    @Override
    public List<T> getTree() {
        return baseMapper.loadTree();
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);
    }
}
